package logic

import (
	"context"
	"errors"
	"user/models/mysql"

	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品创建
func (l *GoodAddLogic) GoodAdd(in *userrpc.GoodAddRequest) (*userrpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FindUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}
	good := mysql.Good{
		GoodName:  in.GoodName,
		GoodPrice: float64(in.GoodPrice),
		GoodImage: in.GoodName,
		GoodStock: in.GoodStock,
		UserId:    in.UserId,
	}
	err = good.GoodAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("商品添加失败")
	}
	return &userrpc.GoodAddResponse{
		GoodId: int64(good.ID),
	}, nil
}
